<?php

namespace App\ZhCN\Api;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use Phoenix\Support\MsgHelper;
use Phoenix\Log\Log4p as logger;

class ContactForm {

    private function __RestController() {}

    private function __Route($value = '/api') {}

    protected function __Inject($db, $session, Repository\ContactForm $contactForm, Service\Mail $mail) {}

    /**
     * 首页联系表单接品
     * @return array
     */
    public function contactForm() {
        $_hash = $this->session->contactHash['hash'];
        $_submitTime = $this->session->contactHash['time'];

        if ($_hash == $_POST['contactHash'] && $_submitTime == 0) {
            if (isset($_POST['contactForm']) && $_POST['contactForm'] != '') {
                if ($flag = $this->contactForm->save($_POST['contactForm'])) {
                    $this->session->contactHash = array('time' => 1);
                    return MsgHelper::err(0);
                } else {
                    return  MsgHelper::err(10003, '系统繁忙!');
                }
            } else {
                return MsgHelper::err(10002, '非法操作!');
            }
        } else {
            return MsgHelper::err(10002, '不能重复提交!');
        }
    }

    public function saveForm()
    {
        if (is_array($_POST) && !empty($_POST)) {
            $data = $_POST;
            $_r = $this->db->table('`#@__@form`')
                ->row(array(
                    '`company_name`' => '?',
                    '`address`' => '?',
                    '`people`' => '?',
                    '`mail`' => '?',
                    '`mobile`' => '?',
                    '`fax`' => '?',
                    '`content`' => '?',
                    '`add_time`' => '?'
                ))
                ->bind(array(
                    $data['company_name'],
                    $data['address'],
                    $data['people'],
                    $data['mail'],
                    $data['mobile'],
                    $data['fax'],
                    $data['content'],
                    time()
                ))
                ->save();
            if ($_r) {
                $mail = 'info@sbt-sh.com';
                $content = '公司名称: ' . $_POST['company_name'] . '<br/>联络地址: ' . $_POST['address'] . '<br/>联系人: ' . $_POST['people'] . '<br/>Email: ' . $_POST['mail'] . '<br/>联系电话: ' . $_POST['mobile'] . '<br/>传真: ' . $_POST['fax'] . '<br/>内容: ' . $_POST['content'];
                $this->mail->send($mail, '在线留言', $content);
                return MsgHelper::err(0, 'ok!');
            } else {
                return  MsgHelper::err(10003, '系统繁忙!');
            }
        } else {
            return MsgHelper::err(10002, '不能重复提交!');
        }
    }
}
